package com.aisile.content.service.impl;
import java.util.List;

import com.aisile.entity.PageResult;
import com.aisile.pojo.TbContentExample;
import com.aisile.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.aisile.mapper.TbContentMapper;
import com.aisile.pojo.TbContent;
import com.aisile.content.service.ContentService;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service(timeout = 40000)
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 查询全部
	 */
	@Override
	public List<TbContent> findAll() {
		return contentMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbContent> page=   (Page<TbContent>) contentMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbContent content) {

		contentMapper.insert(content);

	}

	@Override
	public void update(TbContent content) {
		contentMapper.updateByPrimaryKey(content);

	}


	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbContent findOne(Long id){
		return contentMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id:ids){

			contentMapper.deleteByPrimaryKey(id);

		}
	}


	@Override
	public PageResult findPage(TbContent content, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbContentExample example=new TbContentExample();
		TbContentExample.Criteria criteria = example.createCriteria();
		
		if(content!=null){			
						if(content.getTitle()!=null && content.getTitle().length()>0){
				criteria.andTitleLike("%"+content.getTitle()+"%");
			}
			if(content.getUrl()!=null && content.getUrl().length()>0){
				criteria.andUrlLike("%"+content.getUrl()+"%");
			}
			if(content.getPic()!=null && content.getPic().length()>0){
				criteria.andPicLike("%"+content.getPic()+"%");
			}
			if(content.getStatus()!=null && content.getStatus().length()>0){
				criteria.andStatusLike("%"+content.getStatus()+"%");
			}
	
		}
		
		Page<TbContent> page= (Page<TbContent>)contentMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<TbContent> findContentByParentId(long parentId) {

		//hash  key filed value

		//先根据parentId 去redis当中查询数据
		List<TbContent> list = (List<TbContent>) redisTemplate.boundHashOps(Constants.CONTENT_CACHE_KEY).get(Long.toString(parentId));
		if(list!=null&&list.size()>0){

			//查询到数据
			//直接把查询到的数据作为结果返回
			return list;
		}else {
			//没有查询到数据
			//去数据库查询数据 将查询到的数据 放入到redis
			TbContentExample example = new TbContentExample();
			example.createCriteria().andCategoryIdEqualTo(parentId);
			List<TbContent> tbContents = contentMapper.selectByExample(example);

			redisTemplate.boundHashOps(Constants.CONTENT_CACHE_KEY).put(Long.toString(parentId),tbContents);
			return tbContents;

		}






	}


}
